package edu.bit.water.service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.constraints.Null;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import edu.bit.water.dao.EntityEvaWbalTestDao;

@Service
public class EntityEvaWbalTestService {

	@Autowired
	private EntityEvaWbalTestDao entityEvaWbalTestDao;
	
	/**
	 * 插入數值
	 * @param list 真实數值
	 * @param listColumn 列名
	 * @return succss and failed message
	 * @throws Exception 
	 */
	public Map<String, Object> insertEntityEvaWbalTest(List<Object> list, List<String> listColumn) throws Exception {
		Map<String, Object> insertMap = new HashMap<>();
		Map<String, Object> modelMap = new HashMap<>();
		//List<Map<String, Object>> rs = selectEntityEvaWbalTest();
		
		//固定字段
		String strSum = "CREATED_AT,PROGRAM_NAME";
		for(String str : listColumn)
			strSum += ',' + str;
		strSum += ",DRAINAGE_VOLUME,LOSS_WATER_VOLUME,WATER_CONSUMPTION,COMPANY_ID,USER_ID";//變化字段在固定字段中間
		insertMap.put("lineColumn", strSum);
		insertMap.put("lineList", list);
		try {
			entityEvaWbalTestDao.insertEntityEvaWbalTest(insertMap);
			modelMap.put("status", "success");
		}catch (DuplicateKeyException e){
        	if(e.getMessage().contains("WATER_CLASS")) {
        		modelMap.put("status", "failed");
        		modelMap.put("msg", "水源类别已存在，请重新输入");
        		return modelMap;
        	}
		}
		return modelMap;
	}
	
	/**
	 * 刪除數據
	 * @param listId delete row id
	 * @return 1 成功；0 失败
	 */
	public int deleteEntityEvaWbalTest(List<Integer> listId) {
		try {
			//使用增强for循环进行删除
			for(int id : listId) {
				entityEvaWbalTestDao.deleteEntityEvaWbalTest(id);
			}
			return 1;
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
		
	}
	
	/**
	 * 根據label_id展示評估數據
	 * @param label_id
	 * @return database message
	 */
	public List<Map<String, Object>> selectEntityEvaWbalTest(int label_id) {
		return entityEvaWbalTestDao.selectEntityEvaWbalTest(label_id);
	}
		
	/**
	 * 更新記錄
	 * @param id 記錄id
	 * @param list 記錄數據
	 * @param listColumn 記錄列名
	 * @return 成功返回1，失败返回0
	 */
	public int updateEntityEvaWbalTest(int id, List<Object> list, List<String> listColumn) {
		
		int length = listColumn.size();
		//可變字段在固定字段中間
		String strSum = "CREATED_AT=" + "to_date('" + list.remove(0) + "','yyyy-mm-dd'),";
		strSum += "PROGRAM_NAME=" + "'" + list.remove(0) + "'," ;
		for(int i = 0; i < length; i++) {
			strSum += listColumn.get(i) + "='" + list.remove(0) + "',";
 		}
		strSum += "DRAINAGE_VOLUME=" + "'" + list.remove(0) + "',"+
				"LOSS_WATER_VOLUME=" + "'" + list.remove(0) + "',"+
				"WATER_CONSUMPTION=" + "'" + list.remove(0) + "',";
		strSum = strSum.substring(0, strSum.length() - 1);
		Map<String, Object> sqlMap = new HashMap<>();
		sqlMap.put("id", id);
		sqlMap.put("sql", strSum);
		return entityEvaWbalTestDao.updateEntityEvaWbalTest(sqlMap);
	}
	
}
